import { HttpClient } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { ActivatedRoute, Router } from '@angular/router';

@Component({
  selector: 'app-welcome',
  templateUrl: './welcome.component.html',
  styleUrls: ['./welcome.component.css']
})
export class WelcomeComponent implements OnInit {
  validateForm!: FormGroup;
  loading = false;
  submitted = false;
  passwordVisible = false
  constructor(
    private formBuilder: FormBuilder,
    private route: ActivatedRoute,
    private router: Router,
    private http: HttpClient
  ) {}

  ngOnInit(): void {
    this.validateForm = this.formBuilder.group({
      username: ['', Validators.required],
      password: ['', Validators.required],
    });
  }

  get f() {
    return this.validateForm.controls;
  }

  onSubmit() {
    this.submitted = true;

    // reset alerts on submit
    // stop here if form is invalid
    if (this.validateForm.invalid) {
      return;
    }

    this.loading = true;

    this.http
      .post(
        'http://localhost:6060/oauth/token?grant_type=password&username=ROOT&password=password&client_id=sparrow&client_secret=password',
        { username: 'sysadmin', password: 'password' }
      )
      .subscribe((res:any) => {
        localStorage.setItem("token",res.access_token)
      });
  }

}
